package com.Daisy.swi.client.Data;



public class Manager extends Person {
	private static final long serialVersionUID = 3001406326069242995L;
	private BookList rightBooks = new BookList();
	private ReaderList rightReaders = new ReaderList();

	public Manager(String n, String id) {
		this.name = n;
		this.id = id;
	}

	public Manager(String n, String id, String password) {
		this(n, id);
		this.password = password;
	}

	public BookList searchBook(int way, String str, BookList bookList) {
		/**
		 * way: 1--by name; 2--by writer 3--by ID
		 */
		rightBooks = new BookList();
		boolean found = false;
		switch (way) {
		case 1:
			found = searchBookByName(str, bookList, rightBooks);
			break;
		case 2:
			found = searchBookByWriter(str, bookList, rightBooks);
			break;
		case 3:
			found = searchBookByID(str, bookList, rightBooks);
			break;
		default:
			break;
		}

		if (found) {
			return rightBooks;
		} else {
			return new BookList();
		}
	}

	public boolean searchBookByName(String searchName, BookList bookList,
			BookList rightBooks) {
		boolean found = false;

		for (int i = 0; i < bookList.size(); i++) {
			Book book = bookList.get(i);
			if (searchName.contains(" ")) {
				String[] buf = searchName.split(" ");
				boolean b = true;
				if (buf.length == 0) {
					b = false;
				}
				for (int j = 0; j < buf.length; j++) {
					if (!book.getName().toLowerCase()
							.contains(buf[j].toLowerCase())) {
						b = false;
						break;
					}
				}
				if (b) {
					found = true;
					rightBooks.add(book);
				}
			} else if (book.getName().toLowerCase()
					.contains(searchName.toLowerCase())) {
				rightBooks.add(book);
				found = true;
			}
		}

		return found;
	}

	public boolean searchBookByWriter(String searchWriter, BookList bookList,
			BookList rightBooks) {
		boolean found = false;

		for (int i = 0; i < bookList.size(); i++) {
			Book book = bookList.get(i);
			if (searchWriter.contains(" ")) {
				String[] buf = searchWriter.split(" ");
				boolean b = true;
				if (buf.length == 0) {
					b = false;
				}
				for (int j = 0; j < buf.length; j++) {
					if (!book.getWriter().toLowerCase()
							.contains(buf[j].toLowerCase())) {
						b = false;
						break;
					}
				}
				if (b) {
					found = true;
					rightBooks.add(book);
				}
			} else if (book.getWriter().toLowerCase()
					.contains(searchWriter.toLowerCase())) {
				rightBooks.add(book);
				found = true;
			}
		}

		return found;
	}

	public boolean searchBookByID(String id, BookList bookList,
			BookList rightBooks) {
		boolean found = false;

		for (int i = 0; i < bookList.size(); i++) {
			Book book = bookList.get(i);
			if (id.contains(" ")) {
				String[] buf = id.split(" ");
				boolean b = true;
				if (buf.length == 0) {
					b = false;
				}
				for (int j = 0; j < buf.length; j++) {
					if (!book.getKey().toLowerCase()
							.contains(buf[j].toLowerCase())) {
						b = false;
						break;
					}
				}
				if (b) {
					found = true;
					rightBooks.add(book);
				}
			} else if (book.getKey().toLowerCase().contains(id.toLowerCase())) {
				rightBooks.add(book);
				found = true;
			}
		}

		return found;
	}

	public ReaderList searchReader(int way, String str, ReaderList readerList) {
		/**
		 * way: 1--by name; 2--by ID
		 */
		rightReaders = new ReaderList();
		boolean found = false;
		switch (way) {
		case 1:
			found = searchReaderByName(str, readerList, rightReaders);
			break;
		case 2:
			found = searchReaderByID(str, readerList, rightReaders);
			break;
		default:
			break;
		}

		if (found) {
			return rightReaders;
		} else {
			return new ReaderList();
		}
	}

	public boolean searchReaderByName(String searchName, ReaderList readerList,
			ReaderList rightReaders) {
		boolean found = false;
		for (int i = 0; i < readerList.size(); i++) {
			Reader reader = readerList.get(i);
			if (searchName.contains(" ")) {
				String[] buf = searchName.split(" ");
				boolean b = true;
				if (buf.length == 0) {
					b = false;
				}
				for (int j = 0; j < buf.length; j++) {
					if (!reader.getName().toLowerCase()
							.contains(buf[j].toLowerCase())) {
						b = false;
						break;
					}
				}
				if (b) {
					found = true;
					rightReaders.add(reader);
				}
			} else if (reader.getName().toLowerCase()
					.contains(searchName.toLowerCase())) {
				rightReaders.add(reader);
				found = true;
			}
		}

		return found;
	}

	public boolean searchReaderByID(String id, ReaderList readerList,
			ReaderList rightReaders) {
		boolean found = false;
		for (int i = 0; i < readerList.size(); i++) {
			Reader reader = readerList.get(i);
			if (id.contains(" ")) {
				String[] buf = id.split(" ");
				boolean b = true;
				if (buf.length == 0) {
					b = false;
				}
				for (int j = 0; j < buf.length; j++) {
					if (!reader.getID().toLowerCase()
							.contains(buf[j].toLowerCase())) {
						b = false;
						break;
					}
				}
				if (b) {
					found = true;
					rightReaders.add(reader);
				}
			} else if (reader.getID().toLowerCase().contains(id.toLowerCase())) {
				rightReaders.add(reader);
				found = true;
			}
		}

		return found;
	}

	public void deleteBook(int indexOfBook) {
		deleteBook(indexOfBook, rightBooks);
	}

	public void deleteBook(int index, BookList list) {
		Book book = list.get(index);
		list.remove(book);
	}

	public void deleteBook(Book book, BookList list) {
		for (int i = 0; i < list.size(); i++) {
			if (book == list.get(i)) {
				deleteBook(i, list);
				break;
			}
		}
	}

	public void addABook(BookList bookList, Book book) {
		bookList.add(book);
	}

	public void deleteReader(int indexOfReader) {
		deleteReader(indexOfReader, rightReaders);
	}

	public void deleteReader(int index, ReaderList list) {
		Reader reader = list.get(index);
		list.remove(reader);
	}

	public void deleteReader(Reader reader, ReaderList list) {
		for (int i = 0; i < list.size(); i++) {
			if (reader == list.get(i)) {
				deleteReader(i, list);
				break;
			}
		}
	}

	public boolean addAReader(ReaderList readerList, String name, String id, String password, String institute, int type) {
		boolean isLegal = true;

		for (int i = 0; i < readerList.size(); i++) {
			Reader r = readerList.get(i);
			if (r.getID().equals(id)) {
				isLegal = false;
				break;
			}
		}
		
		if (isLegal) {
			if (type == 3) {
				readerList.add(new Teacher(name, id, password, institute, type));
			} else {
				readerList.add(new Reader(name, id, password, institute, type));
			}
		}
		return isLegal;
	}

	public void setReader(Reader r, ReaderList readerList) {
		readerList.add(r);
	}

	public void resetBook(String name, String writer,
			String publisher, int publishYear, String key, String location,
			int type, int num, String description, int indexOfBook) {
		Book book = this.rightBooks.get(indexOfBook);
		book.setInformation(name, writer, publisher, publishYear, key, location,
				type, num, description);
	}

	public void setRightBooks(BookList rightBooks) {
		this.rightBooks = rightBooks;
	}

	public void setRightReaders(ReaderList rightReaders) {
		this.rightReaders = rightReaders;
	}
}
